﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using GeneralDefine;
using GeneralLib;
using DevExpress.XtraEditors;

namespace TableModule
{
    public enum TableAxisMode
    {
        Stop,
        Job,
        Home,
        AbsMoveSigle,
        RelMove,
        Move,
        Go,
        Jump, 
        Arc,
        Cure
    }
    
    public class TableActioner
    {
        public TableItemSetting tableSetting;
        public TableSingleData tableDate;
        public TableSingleData tableDateGlabal;
        public TableDataSpeed tableMotionSpeed;
        public TableDataTool toolSetting;
        public TableDataLocal localSetting;
        public int iLocalNo;
        public IMotion XAction;
        public IMotion YAction;
        public IMotion ZAction;
        public IMotion UAction;
        private int iXAxisNo;
        private int iYAxisNo;
        private int iZAxisNo;
        private int iUAxisNo;
        
       
        //为门控停止的变量
        public double dTargetPosX;
        public double dTargetPosY;
        public double dTargetPosZ;
        public double dTargetPosU;

        TableDataPosition targetPosition;

        private TableAxisMode tableAxisModeX;
        private TableAxisMode tableAxisModeY;
        private TableAxisMode tableAxisModeZ;
        private TableAxisMode tableAxisModeU;

        private MotionDir jobMoveDirX;
        private MotionDir jobMoveDirY;
        private MotionDir jobMoveDirZ;
        private MotionDir jobMoveDirU;


        public bool bHomeOk = false;
        TableDataPosition CurrentPos;
        private object objLock;
        public TableActioner(TableItemSetting setting, TableSingleData date, TableSingleData dateGlabal)
        {
            objLock = new object();
            tableSetting = setting;
            CurrentPos = new TableDataPosition();
            tableDate = date;
            tableDateGlabal = dateGlabal;
            tableMotionSpeed = new TableDataSpeed();
           
            try
            {
                if (setting.tableAxisSettings[0].bUsed)
                {
                    HardWareBase hardWareX = FrameManager.FrameManager.MotionCard;
                    iXAxisNo = setting.tableAxisSettings[0].iAxisNo;
                    if (hardWareX is IMotion)
                        XAction = (IMotion)hardWareX;
                }
                else
                {
                    XAction = null;
                }
            }
            catch
            {
                XAction = null;
            }
            try
            {
                if (setting.tableAxisSettings[1].bUsed)
                {
                    HardWareBase hardWareY = FrameManager.FrameManager.MotionCard;
                    iYAxisNo = setting.tableAxisSettings[1].iAxisNo;
                    if (hardWareY is IMotion)
                        YAction = (IMotion)hardWareY;
                }
                else
                {
                    YAction = null;
                }
            }
            catch
            {
                YAction = null;
            }

            try
            {
                if (setting.tableAxisSettings[2].bUsed)
                {
                    HardWareBase hardWareZ = FrameManager.FrameManager.MotionCard;
                    iZAxisNo = setting.tableAxisSettings[2].iAxisNo;
                    if (hardWareZ is IMotion)
                        ZAction = (IMotion)hardWareZ;
                }
                else
                {
                    ZAction = null;
                }
            }
            catch
            {
                ZAction = null;
            }

            try
            {
                if (setting.tableAxisSettings[3].bUsed)
                {
                    HardWareBase hardWareU = FrameManager.FrameManager.MotionCard;
                    iUAxisNo = setting.tableAxisSettings[3].iAxisNo;
                    if (hardWareU is IMotion)
                        UAction = (IMotion)hardWareU;
                }
                else 
                {
                    UAction = null;
                }
            }
            catch
            {
                UAction = null;
            }
            toolSetting = new TableDataTool();
            toolSetting.strName = "Default";
            localSetting = new TableDataLocal();
            localSetting.strName = "Default";
            if (date.listTableDataSpeed.Count > 0)
            {
                SetSpd(date.listTableDataSpeed[0]);
            }
            else
            {
                tableMotionSpeed.dAcc = 20.0 *10;
                tableMotionSpeed.dDec = 20.0 * 10;
                tableMotionSpeed.dMoveSpeed = 20.0;
                tableMotionSpeed.dStartSpeed = 10.0;
                tableMotionSpeed.dEndSpeed = 10.0;
                SetSpd(tableMotionSpeed);
            }
        }
        public double GetPosAxis(AxisName axis)
        {
            double dResult = 0.0;
            double iPos=0;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    XAction.GetPos(iXAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[0].plusToUnit;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    YAction.GetPos(iYAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[1].plusToUnit;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    ZAction.GetPos(iZAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[2].plusToUnit;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    UAction.GetPos(iUAxisNo, out iPos);
                    dResult = iPos;//* tableSetting.tableAxisSettings[3].plusToUnit;
                }
            }
            return dResult;
        }
        public void SetPosAxis(AxisName axis,double dPos)
        {

            int iPos = 0;
            if (axis == AxisName.X)
            {
                //iPos = (int)(dPos / tableSetting.tableAxisSettings[0].plusToUnit);
                if (XAction != null)
                {
                    XAction.SetCmdPos(iXAxisNo, dPos);
                    XAction.SetPos(iXAxisNo, dPos);
                }
            }
            if (axis == AxisName.Y)
            {
                //iPos = (int)(dPos / tableSetting.tableAxisSettings[1].plusToUnit);
                if (YAction != null)
                {
                    YAction.SetCmdPos(iYAxisNo, dPos);
                    YAction.SetPos(iYAxisNo, dPos);
                }
            }
            if (axis == AxisName.Z)
            {
                //iPos = (int)(dPos / tableSetting.tableAxisSettings[2].plusToUnit);
                if (ZAction != null)
                {
                    ZAction.SetCmdPos(iZAxisNo, dPos);
                    ZAction.SetPos(iZAxisNo, dPos);
                }
            }
            if (axis == AxisName.U)
            {
                //iPos = (int)(dPos / tableSetting.tableAxisSettings[3].plusToUnit);
                if (UAction != null)
                {
                    UAction.SetCmdPos(iUAxisNo, dPos);
                    UAction.SetPos(iUAxisNo, dPos);
                }
            }
        }
        public double GetCommandAxis(AxisName axis)
        {
            double dResult = 0.0;
            double iPos = 0;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    XAction.GetCmdPos(iXAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[0].plusToUnit;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    YAction.GetCmdPos(iYAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[1].plusToUnit;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    ZAction.GetCmdPos(iZAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[2].plusToUnit;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    UAction.GetCmdPos(iUAxisNo, out iPos);
                    dResult = iPos;// * tableSetting.tableAxisSettings[3].plusToUnit;
                }
            }
            return dResult;
        }
        public TableDataPosition WhereTable()
        {
            CurrentPos.dXPos = GetPosAxis(AxisName.X);
            CurrentPos.dYPos = GetPosAxis(AxisName.Y);
            CurrentPos.dZPos = GetPosAxis(AxisName.Z);
            CurrentPos.dUPos = GetPosAxis(AxisName.U);
            return CurrentPos;
        }
        public TableDataPosition WhereAtToolAndLocal()
        {
            TableDataPosition tempPosTool = new TableDataPosition();
            TableDataPosition tempPos = new TableDataPosition();
            CurrentPos.dXPos = GetPosAxis(AxisName.X);
            CurrentPos.dYPos = GetPosAxis(AxisName.Y);
            CurrentPos.dZPos = GetPosAxis(AxisName.Z);
            CurrentPos.dUPos = GetPosAxis(AxisName.U);
            //CurrentToTool
            SetTool(toolSetting.strName);
            Matrix matrixTool = new Matrix(); 
            matrixTool.Rotate((float)(CurrentPos.dUPos));
            matrixTool.Translate((float)(toolSetting.dX), (float)(toolSetting.dY));
            
            PointF[] pf=new PointF[1];
            pf[0].X=0.0f;
            pf[0].Y=0.0f;
            matrixTool.TransformPoints(pf);
            tempPosTool.dXPos = CurrentPos.dXPos + pf[0].X;
            tempPosTool.dYPos = CurrentPos.dYPos + pf[0].Y;
            tempPosTool.dZPos = CurrentPos.dZPos + toolSetting.dZ;
            tempPosTool.dUPos = CurrentPos.dUPos + toolSetting.dU;
            //CurrentToLocal
            SetLocal(localSetting.strName);
            if (Math.Abs(localSetting.dU) > 0.05)
            {
                Matrix matrixLocal = new Matrix();
                matrixLocal.Translate((float)(localSetting.dX), (float)(localSetting.dY));
                matrixLocal.Rotate((float)(localSetting.dU));
                matrixLocal.Invert();
                PointF[] pfLocal = new PointF[1];
                pfLocal[0].X = (float)(tempPosTool.dXPos);
                pfLocal[0].Y = (float)(tempPosTool.dYPos);
                matrixLocal.TransformPoints(pfLocal);
                tempPos.dXPos = pfLocal[0].X;
                tempPos.dYPos = pfLocal[0].Y;
                tempPos.dZPos = tempPosTool.dZPos + localSetting.dZ;
                //tempPos.dUPos = tempPosTool.dUPos + localSetting.dU;
                //tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
                tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
            }
            else
            {
                double dTempXTest = tempPosTool.dXPos - localSetting.dX;
                double dTempYTest = tempPosTool.dYPos - localSetting.dY;
                double dSinAngle = Math.Sin(localSetting.dU * Math.PI / 180.0);
                double dCosAngle = Math.Cos(localSetting.dU * Math.PI / 180.0);
                tempPos.dXPos = dTempXTest * dCosAngle + dTempYTest * dSinAngle;
                tempPos.dYPos = dTempYTest * dCosAngle - dTempXTest * dSinAngle; 


                tempPos.dZPos = tempPosTool.dZPos + localSetting.dZ;
                //tempPos.dUPos = tempPosTool.dUPos + localSetting.dU;
                //tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
                tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
            }
            if (tableSetting.tableAxisSettings[3].bUsed == false)
            {
                tempPos.dUPos = 0;
            }
            return tempPos;
        }
        public TableDataPosition WhereAtLocal(TableDataPosition position,string strLocalName)
        {
            TableDataPosition tempPosTool = new TableDataPosition();
            TableDataPosition tempPos = new TableDataPosition();
            
            tempPosTool.dXPos = position.dXPos;
            tempPosTool.dYPos = position.dYPos;
            tempPosTool.dZPos = position.dZPos;
            tempPosTool.dUPos = position.dUPos;
            //CurrentToLocal
            TableDataLocal tempLocal=new TableDataLocal();
            
               
            if (tableDateGlabal.dicTableDataLocal.Keys.Contains(strLocalName))
            {
                localSetting.strName = tableDateGlabal.dicTableDataLocal[strLocalName].strName;
                localSetting.dX = tableDateGlabal.dicTableDataLocal[strLocalName].dX;
                localSetting.dY = tableDateGlabal.dicTableDataLocal[strLocalName].dY;
                localSetting.dZ = tableDateGlabal.dicTableDataLocal[strLocalName].dZ;
                localSetting.dU = tableDateGlabal.dicTableDataLocal[strLocalName].dU;
            }
            else
            {
                if (tableDate.dicTableDataLocal.Keys.Contains(strLocalName))
                {
                    localSetting.strName = tableDate.dicTableDataLocal[strLocalName].strName;
                    localSetting.dX = tableDate.dicTableDataLocal[strLocalName].dX;
                    localSetting.dY = tableDate.dicTableDataLocal[strLocalName].dY;
                    localSetting.dZ = tableDate.dicTableDataLocal[strLocalName].dZ;
                    localSetting.dU = tableDate.dicTableDataLocal[strLocalName].dU;
                }
                else
                {
                    localSetting.strName = "Default";
                    localSetting.dX = 0.0;
                    localSetting.dY = 0.0;
                    localSetting.dZ = 0.0;
                    localSetting.dU = 0.0;
                }
            }
           
            if (Math.Abs(localSetting.dU) > 0.05)
            {
                Matrix matrixLocal = new Matrix();
                matrixLocal.Translate((float)(localSetting.dX), (float)(localSetting.dY));
                matrixLocal.Rotate((float)(localSetting.dU));
                matrixLocal.Invert();
                PointF[] pfLocal = new PointF[1];
                pfLocal[0].X = (float)(tempPosTool.dXPos);
                pfLocal[0].Y = (float)(tempPosTool.dYPos);
                matrixLocal.TransformPoints(pfLocal);
                tempPos.dXPos = pfLocal[0].X;
                tempPos.dYPos = pfLocal[0].Y;
                tempPos.dZPos = tempPosTool.dZPos + localSetting.dZ;
                //tempPos.dUPos = tempPosTool.dUPos + localSetting.dU;
                tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
            }
            else
            {
                double dTempXTest = tempPosTool.dXPos - localSetting.dX;
                double dTempYTest = tempPosTool.dYPos - localSetting.dY;
                double dSinAngle = Math.Sin(localSetting.dU * Math.PI / 180.0);
                double dCosAngle = Math.Cos(localSetting.dU * Math.PI / 180.0);
                tempPos.dXPos = dTempXTest * dCosAngle + dTempYTest * dSinAngle;
                tempPos.dYPos = dTempYTest * dCosAngle - dTempXTest * dSinAngle;


                tempPos.dZPos = tempPosTool.dZPos + localSetting.dZ;
                //tempPos.dUPos = tempPosTool.dUPos + localSetting.dU;
                tempPos.dUPos = tempPosTool.dUPos - localSetting.dU;
            }
            return tempPos;
        }
        public bool GetHome(AxisName axis)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult=XAction.GetHome(iXAxisNo);
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.GetHome(iYAxisNo);
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.GetHome(iZAxisNo);
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.GetHome(iUAxisNo);
                }
            }
            return bResult;
        }
        bool bCurAlarmX;
        bool bPreAlarmX;
        bool bCurAlarmY;
        bool bPreAlarmY;
        bool bCurAlarmZ;
        bool bPreAlarmZ;
        bool bCurAlarmU;
        bool bPreAlarmU;
        public bool GetAlarm(AxisName axis)
        {
            bool bResult = false;
            
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.GetAlarm(iXAxisNo);
                    if (bPreAlarmX != bResult)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("X轴马达报警");
                        }
                    }
                    bCurAlarmX = bResult;
                    bPreAlarmX = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreAlarmX = false;
                    //}
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.GetAlarm(iYAxisNo);
                    if (bPreAlarmY != bResult)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("Y轴马达报警");
                        }
                    }
                    bCurAlarmY = bResult;
                    bPreAlarmY = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreAlarmY = false;
                    //}
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.GetAlarm(iZAxisNo);
                    if (bPreAlarmZ != bResult)
                    {
                        XtraMessageBox.Show("Z轴马达报警");
                    }
                    //}
                    bCurAlarmZ = bResult;
                    bPreAlarmZ = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreAlarmZ = false;
                    //}
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.GetAlarm(iUAxisNo);
                    if (bPreAlarmU != bResult)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("U轴马达报警");
                        }
                    }
                    bCurAlarmU = bResult;
                    bPreAlarmU = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreAlarmU = false;
                    //}
                }
            }
            
            return bResult;
        }
        bool bPreCWLX;
        bool bCurCWLX;
        bool bPreCWLY;
        bool bCurCWLY;
        bool bPreCWLZ;
        bool bCurCWLZ;
        bool bPreCWLU;
        bool bCurCWLU;
        public bool GetCWL(AxisName axis)
        {
            bool bResult = false;
            
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.GetCWL(iXAxisNo);
                    if ((bPreCWLX != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("X轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCWLAlarm(tableSetting.strTableName + "_X");
                        }
                    }
                    bCurCWLX = bResult;
                    bPreCWLX = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCWLX = false;
                    //}
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.GetCWL(iYAxisNo);
                    if ((bPreCWLY != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("Y轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCWLAlarm(tableSetting.strTableName + "_Y");
                        }
                    }
                    bCurCWLY = bResult;
                    bPreCWLY = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCWLY = false;
                    //}
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.GetCWL(iZAxisNo);
                    if ((bPreCWLZ != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("Z轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCWLAlarm(tableSetting.strTableName + "_Z");
                        }
                    }
                    bCurCWLZ = bResult;
                    bPreCWLZ = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCWLZ = false;
                    //}
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.GetCWL(iUAxisNo);
                    if ((bPreCWLU != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("U轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCWLAlarm(tableSetting.strTableName + "_U");
                        }
                    }
                    bCurCWLU = bResult;
                    bPreCWLU = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCWLU = false;
                    //}
                }
            }
            
            return bResult;
        }
        bool bPreCCWLX;
        bool bCurCCWLX;
        bool bPreCCWLY;
        bool bCurCCWLY;
        bool bPreCCWLZ;
        bool bCurCCWLZ;
        bool bPreCCWLU;
        bool bCurCCWLU;
        public bool GetCCWL(AxisName axis)
        {
            bool bResult = false;
           
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.GetCCWL(iXAxisNo);
                    if ((bPreCCWLX != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("X轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if(bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCCWLAlarm(tableSetting.strTableName + "_X");
                        }
                    }
                    bCurCCWLX = bResult;
                    bPreCCWLX = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCCWLX = false;
                    //}

                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.GetCCWL(iYAxisNo);
                    if ((bPreCCWLY != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("Y轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCCWLAlarm(tableSetting.strTableName + "_Y");
                        }
                    }

                    bCurCCWLY = bResult;
                    bPreCCWLY = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCCWLY = false;
                    //}
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.GetCCWL(iZAxisNo);
                    if ((bPreCCWLZ != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("Z轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCCWLAlarm(tableSetting.strTableName + "_Z");
                        }
                    }
                    bCurCCWLZ = bResult;
                    bPreCCWLZ = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCCWLZ = false;
                    //}
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.GetCCWL(iUAxisNo);
                    if ((bPreCCWLU != bResult) && bHomeOk)
                    {
                        if (bResult)
                        {
                            XtraMessageBox.Show("U轴极限错误");
                        }
                        else
                        {
                            //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                            //if (bCurResetButton)
                            //    FrmManagement.frmAlarm.RstMotorCCWLAlarm(tableSetting.strTableName + "_U");
                        }
                    }
                    bCurCCWLU = bResult;
                    bPreCCWLU = bResult;
                    //bool bCurResetButton = IOManager.ioSetting.listStandInputs[(int)StandInputName.Reset].ON;
                    //if (bCurResetButton)
                    //{
                    //    bPreCCWLU = false;
                    //}
                }
            }
            
            return bResult;
        }
        public bool GetMoveDone(AxisName axis)
        {
            bool bResult = MoveDone(axis);
            
            return bResult;
        }
        public bool Stop(AxisName axis)
        {
            bool bResult = false;

            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.Stop(iXAxisNo);
                }
                tableAxisModeX = TableAxisMode.Stop;
            }

            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.Stop(iYAxisNo);
                }
                tableAxisModeY = TableAxisMode.Stop;
            }

            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.Stop(iZAxisNo);
                }
                tableAxisModeZ = TableAxisMode.Stop;
            }

            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.Stop(iUAxisNo);
                }
                tableAxisModeU = TableAxisMode.Stop;
            }
            return bResult;
        }
        public bool StopALL()
        {
            bool bResult = false;
            
            if (XAction != null)
            {
                bResult = XAction.Stop(iXAxisNo);
            }
           
            
            if (YAction != null)
            {
                bResult = YAction.Stop(iYAxisNo);
            }
            
           
            if (ZAction != null)
            {
                bResult = ZAction.Stop(iZAxisNo);
            }
            
           
            if (UAction != null)
            {
                bResult = UAction.Stop(iUAxisNo);
            }
            tableAxisModeX = TableAxisMode.Stop;
            tableAxisModeY = TableAxisMode.Stop;
            tableAxisModeZ = TableAxisMode.Stop;
            tableAxisModeU = TableAxisMode.Stop;
            return bResult;
        }
        public bool EstopALL()
        {
            StopALL();
            return false;
        }
        public bool SetSpd(TableDataSpeed speed)
        {
            bool bResult = false;
            MotionSpeed motionSpeed=new MotionSpeed();
            motionSpeed.dAcc = speed.dAcc;
            motionSpeed.dDec = speed.dDec;
            tableMotionSpeed.dAcc = speed.dAcc;
            tableMotionSpeed.dDec = speed.dDec;
            
            tableMotionSpeed.dMoveSpeed = speed.dMoveSpeed;
            tableMotionSpeed.dStartSpeed = speed.dStartSpeed;
            tableMotionSpeed.dEndSpeed = speed.dEndSpeed; ;
            if (XAction != null)
            {
                motionSpeed.dAcc = speed.dAcc;//* 10.0 * 1000 / (tableSetting.tableAxisSettings[0].plusToUnit);
                motionSpeed.dDec = speed.dDec;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[0].plusToUnit);
                motionSpeed.iMoveSpeed = speed.dMoveSpeed;// (int)(speed.dMoveSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.iStartSpeed = speed.dStartSpeed;//(int)(speed.dStartSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.iEndSpeed = speed.dEndSpeed;// (int)(speed.dEndSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                bResult = XAction.SetSpd(iXAxisNo, motionSpeed);
            }
            
            
            if (YAction != null)
            {
                motionSpeed.dAcc = speed.dAcc;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[1].plusToUnit);
                motionSpeed.dDec = speed.dDec;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[1].plusToUnit);

                motionSpeed.iMoveSpeed = speed.dMoveSpeed;// (int)(speed.dMoveSpeed / (tableSetting.tableAxisSettings[1].plusToUnit));
                motionSpeed.iStartSpeed = speed.dStartSpeed;//(int)(speed.dStartSpeed / (tableSetting.tableAxisSettings[1].plusToUnit));
                motionSpeed.iEndSpeed = speed.dEndSpeed;// (int)(speed.dEndSpeed / (tableSetting.tableAxisSettings[1].plusToUnit));
                bResult = YAction.SetSpd(iYAxisNo, motionSpeed);
            }
            
            
            if (ZAction != null)
            {
                motionSpeed.dAcc = speed.dAcc;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[2].plusToUnit);
                motionSpeed.dDec = speed.dDec;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[2].plusToUnit);

                motionSpeed.iMoveSpeed = speed.dMoveSpeed;// (int)(speed.dMoveSpeed / (tableSetting.tableAxisSettings[2].plusToUnit));
                motionSpeed.iStartSpeed = speed.dStartSpeed;// (int)(speed.dStartSpeed / (tableSetting.tableAxisSettings[2].plusToUnit));
                motionSpeed.iEndSpeed = speed.dEndSpeed;// (int)(speed.dEndSpeed / (tableSetting.tableAxisSettings[2].plusToUnit));
                bResult = ZAction.SetSpd(iZAxisNo, motionSpeed);
            }
            
           
            if (UAction != null)
            {
                if (UAction != null)
                {
                    motionSpeed.dAcc = speed.dAcc;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[3].plusToUnit);
                    motionSpeed.dDec = speed.dDec;// * 10.0 * 1000 / (tableSetting.tableAxisSettings[3].plusToUnit);

                    motionSpeed.iMoveSpeed = speed.dMoveSpeed;// (int)(speed.dMoveSpeed / (tableSetting.tableAxisSettings[3].plusToUnit));
                    motionSpeed.iStartSpeed = speed.dStartSpeed;// (int)(speed.dStartSpeed / (tableSetting.tableAxisSettings[3].plusToUnit));
                    motionSpeed.iEndSpeed = speed.dEndSpeed;// (int)(speed.dEndSpeed / (tableSetting.tableAxisSettings[3].plusToUnit));
                    bResult = UAction.SetSpd(iUAxisNo, motionSpeed);
                }
            }
            SetCureSpd(speed);
            return false;
        }
        public bool SetSpd(string strSpdName)
        {
            SetSpd(tableDateGlabal.dicTableDataSpeed[strSpdName]);
            return false;
        }
        public bool Jog(AxisName axis, MotionDir dir)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.Jog(iXAxisNo, dir);
                    tableAxisModeX = TableAxisMode.Job;
                    jobMoveDirX = dir;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.Jog(iYAxisNo, dir);
                    tableAxisModeY = TableAxisMode.Job;
                    jobMoveDirY = dir;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.Jog(iZAxisNo, dir);
                    tableAxisModeZ = TableAxisMode.Job;
                    jobMoveDirZ = dir;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.Jog(iUAxisNo, dir);
                    tableAxisModeU = TableAxisMode.Job;
                    jobMoveDirU = dir;
                }
            }
            return bResult;
        }
        public bool Home(AxisName axis, MotionDir dir, HomeMode homeMode)
        {
            return false;
        }
        public bool Home(AxisName axis)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.Home(iXAxisNo, tableSetting.tableAxisSettings[0].homeDir, tableSetting.tableAxisSettings[0].homeMode);
                    tableAxisModeX = TableAxisMode.Home;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.Home(iYAxisNo, tableSetting.tableAxisSettings[1].homeDir, tableSetting.tableAxisSettings[1].homeMode);
                    tableAxisModeY = TableAxisMode.Home;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.Home(iZAxisNo, tableSetting.tableAxisSettings[2].homeDir, tableSetting.tableAxisSettings[2].homeMode);
                    tableAxisModeZ = TableAxisMode.Home;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.Home(iUAxisNo, tableSetting.tableAxisSettings[3].homeDir, tableSetting.tableAxisSettings[3].homeMode);
                    tableAxisModeU = TableAxisMode.Home;
                }
            }
            return bResult;
        }
        public bool AbsMoveSingle(AxisName axis, double dPos)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.AbsMoveSigle(iXAxisNo, dPos);// (int)(dPos / tableSetting.tableAxisSettings[0].plusToUnit));
                    tableAxisModeX = TableAxisMode.AbsMoveSigle;
                    dTargetPosX = dPos;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.AbsMoveSigle(iYAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[1].plusToUnit));
                    tableAxisModeY = TableAxisMode.AbsMoveSigle;
                    dTargetPosY = dPos;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.AbsMoveSigle(iZAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[2].plusToUnit));
                    tableAxisModeZ = TableAxisMode.AbsMoveSigle;
                    dTargetPosZ = dPos;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.AbsMoveSigle(iUAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[3].plusToUnit));
                    tableAxisModeU = TableAxisMode.AbsMoveSigle;
                    dTargetPosU =dPos;
                }
            }
            return bResult;
        }
        public bool RelMoveSingle(AxisName axis, double dPos)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.RelMoveSigle(iXAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[0].plusToUnit));
                    tableAxisModeX = TableAxisMode.RelMove;
                    dTargetPosX = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.RelMoveSigle(iYAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[1].plusToUnit));
                    tableAxisModeY = TableAxisMode.RelMove;
                    dTargetPosY = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.RelMoveSigle(iZAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[2].plusToUnit));
                    tableAxisModeZ = TableAxisMode.RelMove;
                    dTargetPosZ = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.RelMoveSigle(iUAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[3].plusToUnit));
                    tableAxisModeU = TableAxisMode.RelMove;
                    dTargetPosU = GetPosAxis(axis) + dPos;
                }
            }
            return bResult;
        }
        public bool RelMove(AxisName axis, double dPos)
        {
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.RelMoveSigle(iXAxisNo, dPos);// (int)(dPos/tableSetting.tableAxisSettings[0].plusToUnit));
                    tableAxisModeX = TableAxisMode.RelMove;
                    dTargetPosX = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.RelMoveSigle(iYAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[1].plusToUnit));
                    tableAxisModeY = TableAxisMode.RelMove;
                    dTargetPosY = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.RelMoveSigle(iZAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[2].plusToUnit));
                    tableAxisModeZ = TableAxisMode.RelMove;
                    dTargetPosZ = GetPosAxis(axis) + dPos;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.RelMoveSigle(iUAxisNo, dPos);//  (int)(dPos / tableSetting.tableAxisSettings[3].plusToUnit));
                    tableAxisModeU = TableAxisMode.RelMove;
                    dTargetPosU = GetPosAxis(axis) + dPos;
                }
            }
            return bResult;
        }
        public bool Move(TableDataPosition position)
        {
            targetPosition = new TableDataPosition(position);
            TableDataPosition tempPos = TranTargetToMotor(position);
            if (XAction != null && YAction != null&&ZAction==null && UAction == null)
            {
                //targetPosition = position;
                tableAxisModeX = TableAxisMode.Move;
                tableAxisModeY = TableAxisMode.Move;
                tableAxisModeZ = TableAxisMode.Move;
                tableAxisModeU = TableAxisMode.Move;

                int iPosNo=2;
                double[] iPoses=new double[iPosNo];
                int[] iAxisNoes=new int[iPosNo];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit); ;
                iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                dTargetPosX = tempPos.dXPos;
                dTargetPosY = tempPos.dYPos;
                dTargetPosZ = GetPosAxis(AxisName.Z);
                dTargetPosU = GetPosAxis(AxisName.U);
                iAxisNoes[0]= tableSetting.tableAxisSettings[0].iAxisNo;
                iAxisNoes[1]= tableSetting.tableAxisSettings[1].iAxisNo;
                
                XAction.ABSMove(iAxisNoes, iPoses);
            }
            if (XAction != null && YAction == null && ZAction != null && UAction == null)
            {
                //targetPosition = position;
                tableAxisModeX = TableAxisMode.Move;
                tableAxisModeY = TableAxisMode.Move;
                tableAxisModeZ = TableAxisMode.Move;
                tableAxisModeU = TableAxisMode.Move;

                int iPosNo = 2;
                double[] iPoses = new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                iPoses[1] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                dTargetPosX = tempPos.dXPos;
                dTargetPosY = GetPosAxis(AxisName.Y);
                dTargetPosZ = tempPos.dZPos;
                dTargetPosU = GetPosAxis(AxisName.U);
                iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                iAxisNoes[1] = tableSetting.tableAxisSettings[2].iAxisNo;

                XAction.ABSMove(iAxisNoes, iPoses);
            }
            if (XAction == null && YAction != null && ZAction != null && UAction == null)
            {
                //targetPosition = position;
                tableAxisModeX = TableAxisMode.Move;
                tableAxisModeY = TableAxisMode.Move;
                tableAxisModeZ = TableAxisMode.Move;
                tableAxisModeU = TableAxisMode.Move;

                int iPosNo = 2;
                double[] iPoses = new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];

                iPoses[0] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                iPoses[1] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                dTargetPosX = GetPosAxis(AxisName.X);
                dTargetPosY = tempPos.dYPos;
                dTargetPosZ = tempPos.dZPos;
                dTargetPosU = GetPosAxis(AxisName.U);
                iAxisNoes[0] = tableSetting.tableAxisSettings[1].iAxisNo;
                iAxisNoes[1] = tableSetting.tableAxisSettings[2].iAxisNo;

                YAction.ABSMove(iAxisNoes, iPoses);
            }
            if (XAction != null && YAction != null && ZAction != null&& UAction == null)
            {
                //targetPosition = position;
                tableAxisModeX = TableAxisMode.Move;
                tableAxisModeY = TableAxisMode.Move;
                tableAxisModeZ = TableAxisMode.Move;
                tableAxisModeU = TableAxisMode.Move;
                int iPosNo = 3;
                
                double[] iPoses = new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                iPoses[1] = tempPos.dYPos;//(int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                iPoses[2] = tempPos.dZPos;//(int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                dTargetPosX = tempPos.dXPos;
                dTargetPosY = tempPos.dYPos;
                dTargetPosZ = tempPos.dZPos;
                dTargetPosU = GetPosAxis(AxisName.U);
                iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;
                XAction.ABSMove(iAxisNoes, iPoses);
            }
            if (XAction != null && YAction != null && ZAction != null && UAction != null)
            {
                //targetPosition = position;
                tableAxisModeX = TableAxisMode.Move;
                tableAxisModeY = TableAxisMode.Move;
                tableAxisModeZ = TableAxisMode.Move;
                tableAxisModeU = TableAxisMode.Move;
                int iPosNo = 4;

                double[] iPoses = new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                iPoses[2] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                iPoses[3] = tempPos.dUPos;// (int)(tempPos.dUPos / tableSetting.tableAxisSettings[3].plusToUnit);
                dTargetPosX = tempPos.dXPos;
                dTargetPosY = tempPos.dYPos;
                dTargetPosZ = tempPos.dZPos;
                dTargetPosU = tempPos.dUPos;
                iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;
                iAxisNoes[3] = tableSetting.tableAxisSettings[3].iAxisNo;
                XAction.ABSMove(iAxisNoes, iPoses);
            }
            return true;
        }
        public bool Move(string strPosName)
        {
            if (tableDateGlabal.dicTableDataPos.Keys.Contains(strPosName))
            {
                Move(tableDateGlabal.dicTableDataPos[strPosName]);
                return true;
            }
            if (tableDate.dicTableDataPos.Keys.Contains(strPosName))
            {
                Move(tableDate.dicTableDataPos[strPosName]);
            }
            return true;
        }
        public bool Go(TableDataPosition position)
        {
            //LocalToCurrent
            TableDataPosition tempPos = TranTargetToMotor(position);
            if (XAction != null)
            {
                AbsMoveSingle(AxisName.X, tempPos.dXPos);
            }
            if (YAction != null)
            {
                AbsMoveSingle(AxisName.Y, tempPos.dYPos);
            }
            if (ZAction != null)
            {
                AbsMoveSingle(AxisName.Z, tempPos.dZPos);
            }
            if (UAction != null)
            {
                AbsMoveSingle(AxisName.U, tempPos.dUPos);
            }
            return true;
        }
        public bool Go(string strPosName)
        {
            if (tableDateGlabal.dicTableDataPos.Keys.Contains(strPosName))
            {
                Go(tableDateGlabal.dicTableDataPos[strPosName]);
                return true;
            }
            if (tableDate.dicTableDataPos.Keys.Contains(strPosName))
            {
                Go(tableDate.dicTableDataPos[strPosName]);
            }
            return true; 
        }
        public bool Jump(TableDataPosition position)
        {
            targetPosition = new TableDataPosition(position);
            TableDataPosition tempPos = TranTargetToMotor(position);
            if (ZAction != null&&(XAction != null || YAction != null))
            {
                tableAxisModeX = TableAxisMode.Jump;
                tableAxisModeY = TableAxisMode.Jump;
                tableAxisModeZ = TableAxisMode.Jump;
                tableAxisModeU = TableAxisMode.Jump;
                int iPosNo = 2;
                if (XAction != null && YAction != null)
                {
                    iPosNo = 3;
                }
                if (XAction != null && YAction != null&&UAction != null)
                {
                    iPosNo = 4;
                }
                double[] iPoses = new double[iPosNo + 1];
                int[] iAxisNoes = new int[iPosNo];
                if (iPosNo > 2)
                {
                    if (iPosNo == 3)
                    {
                        iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                        iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                        iPoses[2] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                        iPoses[3] = tempPos.dJumpLimited;// (int)(tempPos.dJumpLimited / tableSetting.tableAxisSettings[2].plusToUnit);
                        dTargetPosX = tempPos.dXPos;
                        dTargetPosY = tempPos.dYPos;
                        dTargetPosZ = tempPos.dZPos;
                        dTargetPosU = GetPosAxis(AxisName.U);
                        iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                        iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                        iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;

                    }
                    else
                    {
                        iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                        iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                        iPoses[2] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                        iPoses[3] = tempPos.dUPos;// (int)(tempPos.dUPos / tableSetting.tableAxisSettings[3].plusToUnit);
                        iPoses[4] = tempPos.dJumpLimited;// (int)(tempPos.dJumpLimited / tableSetting.tableAxisSettings[2].plusToUnit);

                        dTargetPosX = tempPos.dXPos;
                        dTargetPosY = tempPos.dYPos;
                        dTargetPosZ = tempPos.dZPos;
                        dTargetPosU = tempPos.dUPos;

                        iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                        iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                        iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;
                        iAxisNoes[3] = tableSetting.tableAxisSettings[3].iAxisNo;
                    }
                }
                else
                {
                    if(XAction!=null)
                    {
                        iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                        dTargetPosX = tempPos.dXPos;
                        dTargetPosY = GetPosAxis(AxisName.Y);
                        iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                    }
                    if (YAction != null)
                    {
                        iPoses[0] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                        dTargetPosX = GetPosAxis(AxisName.X);
                        dTargetPosY = tempPos.dYPos;
                        iAxisNoes[0] = tableSetting.tableAxisSettings[1].iAxisNo;
                    }
                    iPoses[1] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPoses[2] = tempPos.dJumpLimited;// (int)(tempPos.dJumpLimited / tableSetting.tableAxisSettings[2].plusToUnit);
                    dTargetPosZ = tempPos.dZPos;
                    dTargetPosU = GetPosAxis(AxisName.U);
                    iAxisNoes[1] = tableSetting.tableAxisSettings[2].iAxisNo;
                }
                if (XAction != null)
                {
                    XAction.ABSJump(iAxisNoes, iPoses);
                }
                else
                {
                    YAction.ABSJump(iAxisNoes, iPoses);
                }
                
            }
            return true;
        }
        public bool Jump(string strPosName)
        {
            if (tableDateGlabal.dicTableDataPos.Keys.Contains(strPosName))
            {
                Jump(tableDateGlabal.dicTableDataPos[strPosName]);
                return true;
            }
            if (tableDate.dicTableDataPos.Keys.Contains(strPosName))
            {
                Jump(tableDate.dicTableDataPos[strPosName]);
            }
            return true;
        }
        public TableDataPosition TranTargetToMotor(TableDataPosition posTarget)
        {
            //LocalToCurrent
            TableDataPosition tempPos = new TableDataPosition(posTarget);
            Matrix matrixToLocal = new Matrix();
            TableDataLocal localData = new TableDataLocal();
            //if (posTarget.iLocalNo == 0)
            //{

            //}
            //else
            //{
            //    //localData = tableDateGlabal.listTableDataLocal[posTarget.iLocalNo - 1];
            //    localData = tableDate.listTableDataLocal[posTarget.iLocalNo - 1];
            //}
            if (posTarget.strLocal == "Default")
            {

            }
            else
            {
                if (tableDateGlabal.dicTableDataLocal.ContainsKey(posTarget.strLocal))
                {
                    localData=tableDateGlabal.dicTableDataLocal[posTarget.strLocal];
                    
                }
                else
                {
                    if (tableDate.dicTableDataLocal.ContainsKey(posTarget.strLocal))
                    {
                        localData = tableDate.dicTableDataLocal[posTarget.strLocal];
                    }
                }
            }
            if (Math.Abs(localData.dU) > 0.05)
            {
                matrixToLocal.Translate((float)(localData.dX), (float)(localData.dY));
                matrixToLocal.Rotate((float)(localData.dU));

                PointF[] pf = new PointF[1];
                pf[0].X = (float)(posTarget.dXPos);
                pf[0].Y = (float)(posTarget.dYPos);
                matrixToLocal.TransformPoints(pf);
                tempPos.dXPos = pf[0].X;
                tempPos.dYPos = pf[0].Y;
                tempPos.dZPos = posTarget.dZPos - localData.dZ;
                tempPos.dUPos = posTarget.dUPos;
            }
            else
            {
                double dSinAngle = Math.Sin(localData.dU * Math.PI / 180.0);
                double dCosAngle = Math.Cos(localData.dU * Math.PI / 180.0);
                tempPos.dXPos = localData.dX + posTarget.dXPos * dCosAngle - posTarget.dYPos * dSinAngle;
                tempPos.dYPos = localData.dY + posTarget.dXPos * dSinAngle + posTarget.dYPos * dCosAngle; 
                tempPos.dZPos = posTarget.dZPos - localData.dZ;
                tempPos.dUPos = posTarget.dUPos;
            }

            //CurrentToTool
            TableDataPosition tempPosTool = new TableDataPosition();
            Matrix matrixToTool = new Matrix();
            //matrixToTool.Rotate((float)(posTarget.dUPos+localData.dU));
            if (tableSetting.tableAxisSettings[3].bUsed)
            {
                matrixToTool.Rotate((float)(posTarget.dUPos + localData.dU - toolSetting.dU));
            }
            matrixToTool.Translate((float)(toolSetting.dX), (float)(toolSetting.dY));
            PointF[] pfTool = new PointF[1];
            pfTool[0].X = (float)(0.0);
            pfTool[0].Y = (float)(0.0);
            matrixToTool.TransformPoints(pfTool);
            tempPosTool.dXPos = tempPos.dXPos - pfTool[0].X;
            tempPosTool.dYPos = tempPos.dYPos - pfTool[0].Y;
            tempPosTool.dZPos = tempPos.dZPos - toolSetting.dZ;
            tempPosTool.dUPos = posTarget.dUPos + localData.dU - toolSetting.dU;
            if(tableSetting.tableAxisSettings[3].bUsed==false)
            {
                tempPosTool.dUPos = posTarget.dUPos;
            }
            tempPosTool.dJumpLimited = posTarget.dJumpLimited;
            return tempPosTool;
        }
        public TableDataPosition TranLocalToMotor(TableDataPosition posTarget)
        {
            //LocalToCurrent
            TableDataPosition tempPos = new TableDataPosition(posTarget);
            Matrix matrixToLocal = new Matrix();
            TableDataLocal localData = new TableDataLocal();
            
            if (posTarget.strLocal == "Default")
            {

            }
            else
            {
                if (tableDateGlabal.dicTableDataLocal.ContainsKey(posTarget.strLocal))
                {
                    localData = tableDateGlabal.dicTableDataLocal[posTarget.strLocal];

                }
                else
                {
                    if (tableDate.dicTableDataLocal.ContainsKey(posTarget.strLocal))
                    {
                        localData = tableDate.dicTableDataLocal[posTarget.strLocal];
                    }
                }
            }
            if (Math.Abs(localData.dU) > 0.05)
            {
                matrixToLocal.Translate((float)(localData.dX), (float)(localData.dY));
                matrixToLocal.Rotate((float)(localData.dU));

                PointF[] pf = new PointF[1];
                pf[0].X = (float)(posTarget.dXPos);
                pf[0].Y = (float)(posTarget.dYPos);
                matrixToLocal.TransformPoints(pf);
                tempPos.dXPos = pf[0].X;
                tempPos.dYPos = pf[0].Y;
                tempPos.dZPos = posTarget.dZPos - localData.dZ;
                tempPos.dUPos = posTarget.dUPos;
            }
            else
            {
                double dSinAngle = Math.Sin(localData.dU * Math.PI / 180.0);
                double dCosAngle = Math.Cos(localData.dU * Math.PI / 180.0);
                tempPos.dXPos = localData.dX + posTarget.dXPos * dCosAngle - posTarget.dYPos * dSinAngle;
                tempPos.dYPos = localData.dY + posTarget.dXPos * dSinAngle + posTarget.dYPos * dCosAngle;
                tempPos.dZPos = posTarget.dZPos - localData.dZ;
                tempPos.dUPos = posTarget.dUPos;
            }
            return tempPos;
        }
        public bool Arc(TableDataPosition positionStart, TableDataPosition positionMid, TableDataPosition positionEnd,bool bCW)
        {
            //LocalToCurrent
            TableDataPosition tempPosStart =TranTargetToMotor(positionStart);
            TableDataPosition tempPosMid = TranTargetToMotor(positionMid);
            TableDataPosition tempPosEnd = TranTargetToMotor(positionEnd);
            if (XAction != null && YAction != null)
            {
                int iPosNo = 2;
                if (ZAction != null)
                {
                    iPosNo = 3;
                }
                double[] iPosesStart = new double[iPosNo];
                double[] iPosesMid = new double[iPosNo];
                double[] iPosedEnd = new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];
                if (iPosNo > 2)
                {
                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesStart[2] = tempPosStart.dZPos;// (int)(tempPosStart.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesMid[0] = tempPosMid.dXPos;// (int)(tempPosMid.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesMid[1] = tempPosMid.dYPos;// (int)(tempPosMid.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesMid[2] = tempPosMid.dZPos;// (int)(tempPosMid.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosedEnd[0] = tempPosEnd.dXPos;// (int)(tempPosEnd.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosedEnd[1] = tempPosEnd.dYPos;// (int)(tempPosEnd.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosedEnd[2] = tempPosEnd.dZPos;// (int)(tempPosEnd.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                    iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                    iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;
                }
                else
                {
                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesMid[0] = tempPosMid.dXPos;// (int)(tempPosMid.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesMid[1] = tempPosMid.dYPos;//(int)(tempPosMid.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosedEnd[0] = tempPosEnd.dXPos;// (int)(tempPosEnd.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosedEnd[1] = tempPosEnd.dYPos;// (int)(tempPosEnd.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                    iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                }
                tableAxisModeX = TableAxisMode.Arc;
                tableAxisModeY = TableAxisMode.Arc;
                tableAxisModeZ = TableAxisMode.Arc;
                tableAxisModeU = TableAxisMode.Arc;
                XAction.ABSArc(iAxisNoes, iPosesStart, iPosesMid, iPosedEnd, bCW);
            }
            return true;
           
        }
        public bool Arc(TableDataPosition positionStart, TableDataPosition positionCenter, double dAngle)
        {
            TableDataPosition tempPosStart = TranTargetToMotor(positionStart);
            TableDataPosition tempPosCenter = TranTargetToMotor(positionCenter);
            if (XAction != null && YAction != null)
            {
                int iPosNo = 2;
                if (ZAction != null)
                {
                    iPosNo = 3;
                }
                double[] iPosesStart = new double[iPosNo];
                double[] iPosesCenter= new double[iPosNo];
                int[] iAxisNoes = new int[iPosNo];
                if (iPosNo > 2)
                {
                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesStart[2] = tempPosStart.dZPos;// (int)(tempPosStart.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesCenter[0] = tempPosCenter.dXPos;// (int)(tempPosCenter.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesCenter[1] = tempPosCenter.dYPos;// (int)(tempPosCenter.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesCenter[2] = tempPosCenter.dZPos;// (int)(tempPosCenter.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                    iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                    iAxisNoes[2] = tableSetting.tableAxisSettings[2].iAxisNo;
                }
                else
                {
                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesCenter[0] = tempPosCenter.dXPos;// (int)(tempPosCenter.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesCenter[1] = tempPosCenter.dYPos;// (int)(tempPosCenter.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iAxisNoes[0] = tableSetting.tableAxisSettings[0].iAxisNo;
                    iAxisNoes[1] = tableSetting.tableAxisSettings[1].iAxisNo;
                }
                tableAxisModeX = TableAxisMode.Arc;
                tableAxisModeY = TableAxisMode.Arc;
                tableAxisModeZ = TableAxisMode.Arc;
                tableAxisModeU = TableAxisMode.Arc;
                XAction.ABSArc(iAxisNoes, iPosesStart, iPosesCenter, dAngle);
            }
            return true;
        }
        public bool Arc(string strPosStart, string strPosMid, string strPosEnd, bool bCW)
        {
            Arc(tableDate.dicTableDataPos[strPosStart], tableDate.dicTableDataPos[strPosMid], tableDate.dicTableDataPos[strPosEnd], bCW);
            return true;
        }
        public bool Arc(string strPosStart, string strPosCenter, double dAngle)
        {
            Arc(tableDate.dicTableDataPos[strPosStart], tableDate.dicTableDataPos[strPosCenter], dAngle);
            return false;
        }
        public bool InitCure()
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                //int iGroup = tableSetting.tableAxisSettings[0].iAxisNo / 4;
                XAction.InitCure(tableSetting.iGroupNo);
            }
            return false;
        }
        public bool SetCureSpd(TableDataSpeed speed)
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                bool bResult = false;
                MotionSpeed motionSpeed = new MotionSpeed();
                motionSpeed.dAcc = speed.dAcc;// * 10.0 / ((tableSetting.tableAxisSettings[0].plusToUnit) * (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.dDec = speed.dDec;// * 10.0 / ((tableSetting.tableAxisSettings[0].plusToUnit) * (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.iMoveSpeed = speed.dMoveSpeed;// (int)(speed.dMoveSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.iStartSpeed = speed.dStartSpeed;// (int)(speed.dStartSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                motionSpeed.iEndSpeed = speed.dEndSpeed;// (int)(speed.dEndSpeed / (tableSetting.tableAxisSettings[0].plusToUnit));
                int iGroup = tableSetting.iGroupNo;
                bResult = XAction.SetCureSpd(tableSetting.iGroupNo, motionSpeed);
            }
            return false;
        }
        public bool SetCureSpd(string strSpdName)
        {
            SetCureSpd(tableDate.dicTableDataSpeed[strSpdName]);
            return false;
        }
        public bool PushLineToCure(TableDataPosition position)
        {
            if (XAction != null && YAction != null && ZAction != null && UAction == null)
            {
                TableDataPosition tempPos = TranTargetToMotor(position);
                double[] iPoses = new double[3];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                iPoses[2] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                XAction.PushLineToCure(tableSetting.iGroupNo, iPoses);
            }
            else if (XAction != null && YAction != null && ZAction != null && UAction != null)
            {
                TableDataPosition tempPos = TranTargetToMotor(position);
                double[] iPoses = new double[4];

                iPoses[0] = tempPos.dXPos;// (int)(tempPos.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                iPoses[1] = tempPos.dYPos;// (int)(tempPos.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                iPoses[2] = tempPos.dZPos;// (int)(tempPos.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                iPoses[3] = tempPos.dUPos;// (int)(tempPos.dUPos / tableSetting.tableAxisSettings[3].plusToUnit);
                XAction.PushLineToCure(tableSetting.iGroupNo, iPoses);
            }
            return false;
        }
        public bool PushLineToCure(string strName)
        {
            PushLineToCure(tableDate.dicTableDataPos[strName]);
            return false;
        }
        public bool PushExtDo(int iChannal, int iOnOff)
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                XAction.PushExtDo(tableSetting.iGroupNo, iChannal, iOnOff);
                
            }
            return false;
        }
        public bool PushDelay(double dDelayTime)
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                XAction.PushDelay(tableSetting.iGroupNo, (int)(dDelayTime*1000.0));
            }
            return false;
        }
        public bool PushArcToCure(TableDataPosition positionStart, TableDataPosition positionMid, TableDataPosition positionEnd, bool bCW,double SweeAngle)
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                TableDataPosition tempPosStart = TranTargetToMotor(positionStart);
                TableDataPosition tempPosMid = TranTargetToMotor(positionMid);
                TableDataPosition tempPosEnd = TranTargetToMotor(positionEnd);
                if (XAction != null && YAction != null)
                {
                   
                    double[] iPosesStart = new double[4];
                    double[] iPosesMid = new double[4];
                    double[] iPosedEnd = new double[4];
                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesStart[2] = tempPosStart.dZPos;// (int)(tempPosStart.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesStart[3] = tempPosStart.dUPos;
                    iPosesMid[0] = tempPosMid.dXPos;// (int)(tempPosMid.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesMid[1] = tempPosMid.dYPos;// (int)(tempPosMid.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesMid[2] = tempPosMid.dZPos;// (int)(tempPosMid.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesMid[3] = tempPosMid.dUPos;
                    iPosedEnd[0] = tempPosEnd.dXPos;// (int)(tempPosEnd.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosedEnd[1] = tempPosEnd.dYPos;// (int)(tempPosEnd.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosedEnd[2] = tempPosEnd.dZPos;// (int)(tempPosEnd.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosedEnd[3] = tempPosEnd.dUPos;
                    XAction.PushArcToCure(tableSetting.iGroupNo, iPosesStart, iPosesMid, iPosedEnd, bCW, SweeAngle);
                    
                }
            }
            return false;
        }
        public bool PushArcToCure(TableDataPosition positionStart, TableDataPosition positionCenter, double dAngle)
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                TableDataPosition tempPosStart = TranTargetToMotor(positionStart);
                TableDataPosition tempPosCenter = TranTargetToMotor(positionCenter);
                if (XAction != null && YAction != null)
                {
                    double[] iPosesStart = new double[4];
                    double[] iPosesCenter = new double[4];

                    iPosesStart[0] = tempPosStart.dXPos;// (int)(tempPosStart.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesStart[1] = tempPosStart.dYPos;// (int)(tempPosStart.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesStart[2] = tempPosStart.dZPos;// (int)(tempPosStart.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesStart[3] = tempPosStart.dUPos;// (int)(tempPosStart.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesCenter[0] = tempPosCenter.dXPos;// (int)(tempPosCenter.dXPos / tableSetting.tableAxisSettings[0].plusToUnit);
                    iPosesCenter[1] = tempPosCenter.dYPos;// (int)(tempPosCenter.dYPos / tableSetting.tableAxisSettings[1].plusToUnit);
                    iPosesCenter[2] = tempPosCenter.dZPos;// (int)(tempPosCenter.dZPos / tableSetting.tableAxisSettings[2].plusToUnit);
                    iPosesCenter[3] = tempPosCenter.dUPos;
                    XAction.PushArcToCure(tableSetting.iGroupNo, iPosesStart, iPosesCenter, dAngle);
                    
                }
            }
            return false;
        }
        public bool PushArcToCure(string strPosStart, string strPosMid, string strPosEnd, bool bCW,double dSweeAngle)
        {
            PushArcToCure(tableDate.dicTableDataPos[strPosStart], tableDate.dicTableDataPos[strPosMid], tableDate.dicTableDataPos[strPosEnd], bCW, dSweeAngle);
            return false;
        }
        public bool PushArcToCure(string strPosStart, string strPosCenter, double dAngle)
        {
            PushArcToCure(tableDate.dicTableDataPos[strPosStart], tableDate.dicTableDataPos[strPosCenter], dAngle);
            return false;
        }
        public bool StartCure()
        {
            if (XAction != null && YAction != null && ZAction != null)
            {
                tableAxisModeX = TableAxisMode.Cure;
                tableAxisModeY = TableAxisMode.Cure;
                tableAxisModeZ = TableAxisMode.Cure;
                tableAxisModeU = TableAxisMode.Cure;            
                XAction.StartCure(tableSetting.iGroupNo);
                
            }
            return false;
        }
        public bool GetCureStatus(out bool bMoveDone, out int iItemActioned)
        {
            bMoveDone = false;
            iItemActioned = 0;
            XAction.GetCureStatus(tableSetting.iGroupNo, out bMoveDone, out iItemActioned);
            return true;
        }
        public bool MoveDoneALL()
        {
            bool bResultX = MoveDone(AxisName.X);
            bool bResultY = MoveDone(AxisName.Y);
            bool bResultZ = MoveDone(AxisName.Z);
            bool bResultU = MoveDone(AxisName.U);

            return bResultX && bResultY && bResultZ && bResultU;
        }
        public bool MoveDone(AxisName axis)
        {
            double dMoveDoneSpace = 0.3;
            bool bResult = false;
            if (axis == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.MoveDone(iXAxisNo);
                    #region JobHandle
                    if (tableAxisModeX == TableAxisMode.Job)
                    {
                        bool bResultTemp = false;
                        if (jobMoveDirX == MotionDir.CW && GetCWL(AxisName.X) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }
                        if (jobMoveDirX == MotionDir.CCW && GetCCWL(AxisName.X) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }
                        bResult = bResultTemp;
                    }
                    #endregion
                    #region HomeHandle
                    if (tableAxisModeX == TableAxisMode.Home)
                    {
                        //bool bResultTemp = true;
                        //double dCurrentX=GetPosAxis(AxisName.X);
                        //if (dCurrentX > -1 && dCurrentX < 1 && bResult)
                        //{
                        //    bResultTemp = true;
                        //    tableAxisModeX = TableAxisMode.Stop;
                        //}
                       
                        //bResult = bResultTemp;
                    }
                    #endregion
                    #region AbsMoveSigleHandle
                    if (tableAxisModeX == TableAxisMode.AbsMoveSigle)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region RelMoveHandle
                    if (tableAxisModeX == TableAxisMode.RelMove)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        if (bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Move
                    if (tableAxisModeX == TableAxisMode.Move)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Jump
                    if (tableAxisModeX == TableAxisMode.Jump)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeX = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                }
                else
                {
                    bResult = true;
                }
            }
            if (axis == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.MoveDone(iYAxisNo);
                    #region JobHandle
                    if (tableAxisModeY == TableAxisMode.Job)
                    {
                        bool bResultTemp = false;
                        if (jobMoveDirY == MotionDir.CW && GetCWL(AxisName.Y) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }
                        if (jobMoveDirY == MotionDir.CCW && GetCCWL(AxisName.Y) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }
                        bResult = bResultTemp;
                    }
                    #endregion
                    #region HomeHandle
                    if (tableAxisModeY == TableAxisMode.Home)
                    {
                        //bool bResultTemp = true;
                        //double dCurrentY = GetPosAxis(AxisName.Y);
                        //if (dCurrentY > -1 && dCurrentY < 1 && bResult)
                        //{
                        //    bResultTemp = true;
                        //    tableAxisModeY = TableAxisMode.Stop;
                        //}

                        //bResult = bResultTemp;
                    }
                    #endregion
                    #region AbsMoveSigleHandle
                    if (tableAxisModeY == TableAxisMode.AbsMoveSigle)
                    {
                        bool bResultTemp = false;
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        if (Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region RelMoveHandle
                    if (tableAxisModeY == TableAxisMode.RelMove)
                    {
                        bool bResultTemp = false;
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        if (bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region MoveHandle
                    if (tableAxisModeY == TableAxisMode.Move)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Jump
                    if (tableAxisModeY == TableAxisMode.Jump)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeY = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                }
                else
                {
                    bResult = true;
                }
            }
            if (axis == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.MoveDone(iZAxisNo);
                    #region JobHandle
                    if (tableAxisModeZ == TableAxisMode.Job)
                    {
                        bool bResultTemp = false;
                        if (jobMoveDirZ == MotionDir.CW && GetCWL(AxisName.Z) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }
                        if (jobMoveDirZ == MotionDir.CCW && GetCCWL(AxisName.Z) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }
                        bResult = bResultTemp;
                    }
                    #endregion
                    #region HomeHandle
                    if (tableAxisModeZ == TableAxisMode.Home)
                    {
                        //bool bResultTemp = true;
                        //double dCurrentZ = GetPosAxis(AxisName.Z);
                        //if (dCurrentZ > -1 && dCurrentZ < 1 && bResult)
                        //{
                        //    bResultTemp = true;
                        //    tableAxisModeZ = TableAxisMode.Stop;
                        //}

                        //bResult = bResultTemp;
                    }
                    #endregion
                    #region AbsMoveSigleHandle
                    if (tableAxisModeZ == TableAxisMode.AbsMoveSigle)
                    {
                        bool bResultTemp = false;
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        if (Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region RelMoveHandle
                    if (tableAxisModeZ == TableAxisMode.RelMove)
                    {
                        bool bResultTemp = false;
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        if (bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Move
                    if (tableAxisModeZ == TableAxisMode.Move)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Jump
                    if (tableAxisModeZ == TableAxisMode.Jump)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeZ = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                }
                else
                {
                    bResult = true;
                }
            }
            if (axis == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.MoveDone(iUAxisNo);
                    #region JobHandle
                    if (tableAxisModeU == TableAxisMode.Job)
                    {
                        bool bResultTemp = false;
                        if (jobMoveDirU == MotionDir.CW && GetCWL(AxisName.U) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }
                        if (jobMoveDirU == MotionDir.CCW && GetCCWL(AxisName.U) && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }
                        bResult = bResultTemp;
                    }
                    #endregion
                    #region HomeHandle
                    if (tableAxisModeU == TableAxisMode.Home)
                    {
                        //bool bResultTemp = true;
                        //double dCurrentU = GetPosAxis(AxisName.U);
                        //if (dCurrentU > -1 && dCurrentU < 1 && bResult)
                        //{
                        //    bResultTemp = true;
                        //    tableAxisModeU = TableAxisMode.Stop;
                        //}

                        //bResult = bResultTemp;
                    }
                    #endregion
                    #region AbsMoveSigleHandle
                    if (tableAxisModeU == TableAxisMode.AbsMoveSigle)
                    {
                        bool bResultTemp = false;
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region RelMoveHandle
                    if (tableAxisModeU == TableAxisMode.RelMove)
                    {
                        bool bResultTemp = false;
                        double dCurrentU= GetPosAxis(AxisName.U);
                        if (bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region MoveHandle
                    if (tableAxisModeU == TableAxisMode.Move)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                    #region Jump
                    if (tableAxisModeU == TableAxisMode.Jump)
                    {
                        bool bResultTemp = false;
                        double dCurrentX = GetPosAxis(AxisName.X);
                        double dCurrentY = GetPosAxis(AxisName.Y);
                        double dCurrentZ = GetPosAxis(AxisName.Z);
                        double dCurrentU = GetPosAxis(AxisName.U);
                        if (Math.Abs(dCurrentX - dTargetPosX) < dMoveDoneSpace &&
                            Math.Abs(dCurrentY - dTargetPosY) < dMoveDoneSpace &&
                            Math.Abs(dCurrentZ - dTargetPosZ) < dMoveDoneSpace &&
                            Math.Abs(dCurrentU - dTargetPosU) < dMoveDoneSpace && bResult)
                        {
                            bResultTemp = true;
                            tableAxisModeU = TableAxisMode.Stop;
                        }

                        bResult = bResultTemp;
                    }
                    #endregion
                }
                else
                {
                    bResult = true;
                }
            }
            return bResult;
        }
        public void SetTool(string strToolName)
        {
            if (strToolName == "Default")
            {
                toolSetting.strName = "Default";
                toolSetting.dX = 0.0;
                toolSetting.dY = 0.0;
                toolSetting.dZ = 0.0;
                toolSetting.dU = 0.0;
            }
            else
            {
                if (tableDateGlabal.dicTableDataTool.Keys.Contains(strToolName))
                {
                    toolSetting.strName = tableDateGlabal.dicTableDataTool[strToolName].strName;
                    toolSetting.dX = tableDateGlabal.dicTableDataTool[strToolName].dX;
                    toolSetting.dY = tableDateGlabal.dicTableDataTool[strToolName].dY;
                    toolSetting.dZ = tableDateGlabal.dicTableDataTool[strToolName].dZ;
                    toolSetting.dU = tableDateGlabal.dicTableDataTool[strToolName].dU;
                }
                else
                {
                    toolSetting.strName = "Default";
                    toolSetting.dX = 0.0;
                    toolSetting.dY = 0.0;
                    toolSetting.dZ = 0.0;
                    toolSetting.dU = 0.0;
                }
            }
        }
        public void SetLocal(string strLocalName)
        {
            if (strLocalName == "Default")
            {
                localSetting.strName = "Default";
                localSetting.dX = 0.0;
                localSetting.dY = 0.0;
                localSetting.dZ = 0.0;
                localSetting.dU = 0.0;
            }
            else
            {
                if (tableDateGlabal.dicTableDataLocal.Keys.Contains(strLocalName))
                {
                    localSetting.strName = tableDateGlabal.dicTableDataLocal[strLocalName].strName;
                    localSetting.dX = tableDateGlabal.dicTableDataLocal[strLocalName].dX;
                    localSetting.dY = tableDateGlabal.dicTableDataLocal[strLocalName].dY;
                    localSetting.dZ = tableDateGlabal.dicTableDataLocal[strLocalName].dZ;
                    localSetting.dU = tableDateGlabal.dicTableDataLocal[strLocalName].dU;
                }
                else
                {
                    if (tableDate.dicTableDataLocal.Keys.Contains(strLocalName))
                    {
                        localSetting.strName = tableDate.dicTableDataLocal[strLocalName].strName;
                        localSetting.dX = tableDate.dicTableDataLocal[strLocalName].dX;
                        localSetting.dY = tableDate.dicTableDataLocal[strLocalName].dY;
                        localSetting.dZ = tableDate.dicTableDataLocal[strLocalName].dZ;
                        localSetting.dU = tableDate.dicTableDataLocal[strLocalName].dU;
                    }
                    else
                    {
                        localSetting.strName = "Default";
                        localSetting.dX = 0.0;
                        localSetting.dY = 0.0;
                        localSetting.dZ = 0.0;
                        localSetting.dU = 0.0;
                    }
                }
            }
        }

        

        public void UpdateLocal(string localName, double dMark1X, double dMark1Y, double dMark2X, double dMark2Y, double dStandardX, double dStandardY)
        {
            double dAngleCurrent = (Math.Atan2(dMark2Y - dMark1Y, dMark2X - dMark1X)) * 180.0 / Math.PI;
            double dAngleStandard = Math.Atan2(dStandardY, dStandardX) * 180.0 / Math.PI;
            double dU = dAngleCurrent - dAngleStandard;
            if (tableDateGlabal.dicTableDataLocal.ContainsKey(localName))
            {
                tableDateGlabal.dicTableDataLocal[localName].dX = dMark1X;
                tableDateGlabal.dicTableDataLocal[localName].dY = dMark1Y;
                tableDateGlabal.dicTableDataLocal[localName].dU = dU;
            }
            else
            {
                if (tableDate.dicTableDataLocal.ContainsKey(localName))
                {
                    tableDate.dicTableDataLocal[localName].dX = dMark1X;
                    tableDate.dicTableDataLocal[localName].dY = dMark1Y;
                    tableDate.dicTableDataLocal[localName].dU = dU;
                }
                else
                {
                    TableDataLocal newLocalData = new TableDataLocal();
                    newLocalData.dX = dMark1X;
                    newLocalData.dY = dMark1Y;
                    newLocalData.dU = dU;
                    newLocalData.strName = localName;
                    tableDate.AddNewLocal(newLocalData);
                    
                }
            }
        }
        public void UpdateLocal(string localName, double dLocalOrgX, double dLocalOrgY, double dLocalOrgZ, double dLocalOrgU)
        {
           
            double dU = dLocalOrgU;
            if (tableDateGlabal.dicTableDataLocal.ContainsKey(localName))
            {
                tableDateGlabal.dicTableDataLocal[localName].dX = dLocalOrgX;
                tableDateGlabal.dicTableDataLocal[localName].dY = dLocalOrgY;
                tableDateGlabal.dicTableDataLocal[localName].dZ = dLocalOrgZ;
                tableDateGlabal.dicTableDataLocal[localName].dU = dU;
            }
            else
            {
                if (tableDate.dicTableDataLocal.ContainsKey(localName))
                {
                    tableDate.dicTableDataLocal[localName].dX = dLocalOrgX;
                    tableDate.dicTableDataLocal[localName].dY = dLocalOrgY;
                    tableDate.dicTableDataLocal[localName].dZ = dLocalOrgZ;
                    tableDate.dicTableDataLocal[localName].dU = dU;
                }
                else
                {
                    TableDataLocal newLocalData = new TableDataLocal();
                    newLocalData.dX = dLocalOrgX;
                    newLocalData.dY = dLocalOrgY;
                    newLocalData.dZ = dLocalOrgZ;
                    newLocalData.dU = dU;
                    newLocalData.strName = localName;
                    tableDate.AddNewLocal(newLocalData);
                    //TableManager.Save();
                }
            }
        }
        public void UpdateLocal(string localName, double dMark1X, double dMark1Y, double dMark2X, double dMark2Y, double dStandardX1, double dStandardY1, double dStandardX2, double dStandardY2)
        {
            double dAngleCurrent = (Math.Atan2(dMark2Y - dMark1Y, dMark2X - dMark1X)) * 180.0 / Math.PI;
            double dAngleStandard = Math.Atan2(dStandardY2-dStandardY1, dStandardX2-dStandardX1) * 180.0 / Math.PI;
            double dU = dAngleCurrent - dAngleStandard;

            double dSinAngle = Math.Sin(dU * Math.PI / 180.0);
            double dCosAngle = Math.Cos(dU * Math.PI / 180.0);
            double dXTemp= dStandardX1* dCosAngle - dStandardY1 * dSinAngle;
            double dYTemp = dStandardX1 * dSinAngle + dStandardY1 * dCosAngle;
            double dOrgX = dMark1X - dXTemp;
            double dOrgY = dMark1Y - dYTemp;
            if (tableDateGlabal.dicTableDataLocal.ContainsKey(localName))
            {
                tableDateGlabal.dicTableDataLocal[localName].dX = dMark1X;
                tableDateGlabal.dicTableDataLocal[localName].dY = dMark1Y;
                tableDateGlabal.dicTableDataLocal[localName].dU = dU;
            }
            else
            {
                if (tableDate.dicTableDataLocal.ContainsKey(localName))
                {
                    tableDate.dicTableDataLocal[localName].dX = dOrgX;
                    tableDate.dicTableDataLocal[localName].dY = dOrgY;
                    tableDate.dicTableDataLocal[localName].dU = dU;
                }
                else
                {
                    TableDataLocal newLocalData = new TableDataLocal();
                    newLocalData.dX = dOrgX;
                    newLocalData.dY = dOrgY;
                    newLocalData.dU = dU;
                    newLocalData.strName = localName;
                    tableDate.AddNewLocal(newLocalData);
                    //TableManager.Save();
                }
            }
        }
        public void UpdateTool(string toolName, double dToolX, double dToolY)
        {
            double dXCurrent=WhereTable().dXPos;
            double dYCurrent=WhereTable().dYPos;
            double dAngleCurrent=WhereTable().dUPos;
            double dR=Math.Sqrt((dToolX-dXCurrent)*(dToolX-dXCurrent)+(dToolY-dYCurrent)*(dToolY-dYCurrent));
            double dAngleTool = Math.Atan2(dToolY - dYCurrent, dToolX - dXCurrent) * 180.0 / Math.PI;
            double dAngle=dAngleTool-dAngleCurrent;
            double dX=dR*Math.Cos(dAngle*Math.PI/180.0);
            double dY=dR*Math.Sin(dAngle*Math.PI/180.0);
            if (tableDateGlabal.dicTableDataTool.ContainsKey(toolName))
            {
                tableDateGlabal.dicTableDataTool[toolName].dX = dX;
                tableDateGlabal.dicTableDataTool[toolName].dY = dY;
                tableDateGlabal.dicTableDataTool[toolName].dU = 0;
            }
            else
            {
                TableDataTool newToolData = new TableDataTool();
                newToolData.dX = dX;
                newToolData.dY = dY;
                newToolData.dU =0;
                newToolData.strName = toolName;
                tableDateGlabal.AddNewTool(newToolData);
                //TableManager.Save();
            }
        }
        public void UpdateTool(string toolName, double dToolX, double dToolY,double dXCurrent, double dYCurrent, double dAngleCurrent)
        {
            double dR = Math.Sqrt((dToolX - dXCurrent) * (dToolX - dXCurrent) + (dToolY - dYCurrent) * (dToolY - dYCurrent));
            double dAngleTool = Math.Atan2(dToolY - dYCurrent, dToolX - dXCurrent) * 180.0 / Math.PI;
            double dAngle = dAngleTool - dAngleCurrent;
            double dX = dR * Math.Cos(dAngle * Math.PI / 180.0);
            double dY = dR * Math.Sin(dAngle * Math.PI / 180.0);
            if (tableDateGlabal.dicTableDataTool.ContainsKey(toolName))
            {
                tableDateGlabal.dicTableDataTool[toolName].dX = dX;
                tableDateGlabal.dicTableDataTool[toolName].dY = dY;
                tableDateGlabal.dicTableDataTool[toolName].dU = 0;
            }
            else
            {
                TableDataTool newToolData = new TableDataTool();
                newToolData.dX = dX;
                newToolData.dY = dY;
                newToolData.dU = 0;
                newToolData.strName = toolName;
                tableDateGlabal.AddNewTool(newToolData);
                //TableManager.Save();
            }
        }
        public bool SetCatchPosInput(int iChannal,int inputChannel,AxisName axisName,bool bRaisedEdge)
        {
            bool bResult = false;
            if(axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult=XAction.SetCatchPosInput(iChannal, inputChannel, iXAxisNo, bRaisedEdge);
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.SetCatchPosInput(iChannal, inputChannel, iYAxisNo, bRaisedEdge);
                }
            }
            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.SetCatchPosInput(iChannal, inputChannel, iZAxisNo, bRaisedEdge);
                }
            }
            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.SetCatchPosInput(iChannal, inputChannel, iUAxisNo, bRaisedEdge);
                }
            }
            return bResult;
        }
        public bool ResetCatchPosInput(int iChannal, AxisName axisName)
        {
            bool bResult = false;
            if (axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.ResetCatchPosInput(iChannal);
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.ResetCatchPosInput(iChannal);
                }
            }

            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.ResetCatchPosInput(iChannal);
                }
            }

            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.ResetCatchPosInput(iChannal);
                }
            }
            return bResult;
        }
        public bool GetCatchPos(int iChannal,out double dAixsPos, AxisName axisName)
        {
            bool bResult = false;
            dAixsPos = 0.0;
            double iPosCatched = 0;
            if (axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.GetCatchPos(iChannal,out iPosCatched);
                    dAixsPos = iPosCatched * tableSetting.tableAxisSettings[0].plusToUnit;
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = XAction.GetCatchPos(iChannal, out iPosCatched);
                    dAixsPos = iPosCatched * tableSetting.tableAxisSettings[1].plusToUnit;
                }
            }

            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = XAction.GetCatchPos(iChannal, out iPosCatched);
                    dAixsPos = iPosCatched * tableSetting.tableAxisSettings[2].plusToUnit;
                }
            }

            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = XAction.GetCatchPos(iChannal, out iPosCatched);
                    dAixsPos = iPosCatched * tableSetting.tableAxisSettings[3].plusToUnit;
                }
            }
            
            return bResult;
        }

        public bool GetServoOn(AxisName axisName)
        {
            bool bResult = false;
            if (axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.GetServoOn(iXAxisNo);
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.GetServoOn(iYAxisNo);
                }
            }

            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.GetServoOn(iZAxisNo);
                }
            }

            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.GetServoOn(iUAxisNo);
                }
            }
            return bResult;
        }
        public bool RstAlarm(AxisName axisName)
        {
            bool bResult = false;
            if (axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.RsAlarm(iXAxisNo);
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.RsAlarm(iYAxisNo);
                }
            }

            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.RsAlarm(iZAxisNo);
                }
            }

            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.RsAlarm(iUAxisNo);
                }
            }
            return bResult;
        }
        public bool SetServoOn(AxisName axisName,bool bOn)
        {
            bool bResult = false;
            if (axisName == AxisName.X)
            {
                if (XAction != null)
                {
                    bResult = XAction.SetServoOn(iXAxisNo,bOn);
                }
            }
            if (axisName == AxisName.Y)
            {
                if (YAction != null)
                {
                    bResult = YAction.SetServoOn(iYAxisNo, bOn);
                }
            }

            if (axisName == AxisName.Z)
            {
                if (ZAction != null)
                {
                    bResult = ZAction.SetServoOn(iZAxisNo, bOn);
                }
            }

            if (axisName == AxisName.U)
            {
                if (UAction != null)
                {
                    bResult = UAction.SetServoOn(iUAxisNo, bOn);
                }
            }
            return bResult;
        }
    }
}
